package org.idea.spring.framework.event;

import java.util.EventListener;
import java.util.EventObject;
import java.util.Observable;
import java.util.Observer;

/**
 * @Author linhao
 * @Date created in 3:52 下午 2021/5/23
 */
public class EventDemo {

    public static void main(String[] arg) {
        EventObservable observable = new EventObservable();
        observable.addObserver(new EventObserver());
        observable.notifyObservers("send message");
    }

    static class EventObservable extends  Observable {
        @Override
        protected synchronized void setChanged() {
            super.setChanged();
        }

        @Override
        public void notifyObservers(Object data){
            this.setChanged();
            super.notifyObservers(new EventObject(data));
            clearChanged();
        }
    }

    static class EventObserver implements Observer, EventListener {

        @Override
        public void update(Observable o, Object msg) {
            EventObject eventObject = (EventObject) msg;
            System.out.println("接收数据：" + eventObject);
        }
    }

}
